package com.zhuhjay.chat.client;

import com.zhuhjay.chat.client.proxy.RpcServiceProxy;
import com.zhuhjay.chat.service.HelloService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ZhuHJay
 * @date 2023/3/10 21:50
 */
@Slf4j
public class RpcClientApplication {
    public static void main(String[] args) {
        ClientBoot clientBoot = new ClientBoot().run(true);
        Channel channel = clientBoot.getChannel();
        log.info("RPC client started ok!");
        /* 初级版： 调用繁琐，需要知道所有的调用信息
        channel.writeAndFlush(new RpcRequestMessage(
                GenericSequenceId.nextId(),
                "com.zhuhjay.chat.service.HelloService",
                "say",
                String.class,
                new Class[]{String.class},
                new Object[]{"张三"}
        )).addListener(future -> {
            // 监听消息抛出的异常信息，否则多线程下无法检测到
            if (!future.isSuccess()) {
                Throwable cause = future.cause();
                log.error("error", cause);
            }
        });*/
        // 远调优化，使用代理
        HelloService service = RpcServiceProxy.getServiceProxy(HelloService.class, channel);
        System.out.println(service.say("张三"));
        System.out.println(service.say("李四"));
        System.out.println(service.say("王五"));
        System.out.println(service.say("赵六"));
        System.out.println(service.say("ZhuHJay"));
    }
}
